package selef.xiafan.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import selef.xiafan.entity.Customer;
import selef.xiafan.entity.system.SysPermission;
import selef.xiafan.entity.user.User;
import selef.xiafan.query.CustomerQuery;
import selef.xiafan.service.ICustomerService;
import selef.xiafan.service.IUserService;
import selef.xiafan.util.result.PageResult;
import selef.xiafan.util.result.Result;
import selef.xiafan.util.result.ResultFactory;

import java.text.ParseException;
import java.util.List;

/**
 * @author xiafan
 * @version 1.0
 * @date 2021/8/16 20:01
 */
@RestController
@RequestMapping("/api/customer")
public class CustomerController {
    @Autowired
    private ICustomerService iCustomerService;
    @Autowired
    IUserService iUserService;

    /*
     * description:客户注册
     * */
    @PostMapping("/register")
    public Result CustomerRegister(@RequestBody Customer requestCustomer) throws ParseException {
        Integer result = iCustomerService.register(requestCustomer);
        if (result == 1) {
            return ResultFactory.buildSuccessResult("注册成功");
        } else if (result == 2) {
            return ResultFactory.buildFailResult(requestCustomer.getPhone() + "该手机号已注册");
        }
        return ResultFactory.buildFailResult("系统异常");
    }

    /*
     * description: 获取客户能访问的菜单
     * */
    @GetMapping("/menu")
    public Result customerMenu() {
        List<SysPermission> menus = iCustomerService.getCustomerMenus();
        return ResultFactory.buildSuccessResult(menus);
    }

    /*
     * description:获取客户信息
     * */
    @PostMapping("/listCustomer")
    public PageResult listCustomer(@RequestBody CustomerQuery customerQuery) {
        return iCustomerService.getCustomerList(customerQuery);
    }

    @PostMapping("/changeIsVip")
    public Result changeVip(@RequestBody Customer customer) {
        Integer result = iCustomerService.changeVip(customer);
        if (result == 1)
            return ResultFactory.buildSuccessResult("已将" + customer.getName() + "升级为Vip用户");
        if (result == 2)
            return ResultFactory.buildSuccessResult("已将" + customer.getName() + "取消Vip身份");
        return ResultFactory.buildFailResult("系统错误");
    }

    @PostMapping("/addWithUpdate")
    public Result AddUpdate(@RequestBody Customer customer){
        Integer result = iCustomerService.AddUpdate(customer);
        if(result ==1)
            return ResultFactory.buildSuccessResult("新客户添加成功");
        if(result==2)
            return ResultFactory.buildSuccessResult("客户信息修改成功");
        return ResultFactory.buildFailResult("操作异常");
    }

    /*
    * 修改密码
    * */
    @PostMapping("/changePass")
    public Result ChangePass(@RequestBody User user){
        iCustomerService.changePass(user);
        return ResultFactory.buildSuccessResult("成功修改密码，当前用户将于5秒后注销，请重新登录");
    }

}
